#include "PopupLayer.h"
#include "LocalizedStringGetter.h"
#include "TitleMenuItem.h"
USING_NS_CC;

Scene * PopupLayer::scene()
{
	Scene * scene = NULL;
	scene = Scene::create();
	PopupLayer * layer = PopupLayer::create();
	scene->addChild(layer);

	return scene;
}

bool PopupLayer::init()
{
	bool bRet = false;
	if (!Layer::init()) {
		return false;
	}

	Size winSize = Director::sharedDirector()->getWinSize();

	//设置这个层的背景图片，并且设置其位置为整个屏幕的中点
	Sprite * background = Sprite::create("InfoBg.png");
	m_bgSprite = background;
	background->setPosition(Point(winSize.width/2,winSize.height/2));
	this->addChild(background);

	//获得背景图片的大小
	Size contentSize = background->getContentSize();
	m_size = contentSize;

	//添加俩个菜单在这个层中
//	MenuItemImage * item1 = MenuItemImage::create("CloseNormal.png","CloseSelected.png","CloseSelected.png",
//		this,menu_selector(PopupLayer::yesButton));

    auto okitem = TitleMenuItem::create("WinButtonBg.png", "WinButtonSelected.png","WinButtonBg.png", CC_CALLBACK_1(PopupLayer::yesButton, this));
    okitem->setAnchorPoint(Vec2::ANCHOR_MIDDLE_TOP);
    okitem->setTitle(LocalizedStringGetter::getStringByKey("Ok"), Size(135,50));

//	MenuItemImage * item2 = MenuItemImage::create("MapBollBlue.png","MapBollBlue.png","",
//		this,menu_selector(PopupLayer::noButton));

	Menu * menu = Menu::create(okitem, NULL);
	menu->alignItemsHorizontallyWithPadding(5);
	menu->setPosition(Point(contentSize.width / 2,contentSize.height / 2));
	//kCCMenuHandlerPriority的值为-128，代表的是菜单按钮的触摸优先级
	//设置menu优先级，这里设置为普通menu的二倍减一，原因看下边
	//menu->setTouchPriority(-128 * 2 - 1);

	background->addChild(menu);

	//设置题目和文本内容
//	this->setTitle();
//	this->setContent();

	this->setTouchEnabled(false);
	bRet = true;

	return bRet;
}

//点击菜单按钮的时候调用的事件处理函数
void PopupLayer::yesButton(Object * object)
{
	this->removeFromParentAndCleanup(true);
}

void PopupLayer::noButton(Object * object)
{
	this->removeFromParentAndCleanup(true);
}

//设置这个层的题目
void PopupLayer::setTitle(const char *titleString)
{
	LabelTTF * title = LabelTTF::create(titleString,"Arial",10);
	//LabelBMFont * title = LabelBMFont::create("Tips","bitmapFontChinese.fnt");
	title->setPosition(Point(m_size.width / 2,m_size.height - title->getContentSize().height / 2));
	m_bgSprite->addChild(title);
}

//设置层的内容
void PopupLayer::setContent(const char *contentString)
{
	LabelTTF * content = LabelTTF::create(contentString,"Arial",14);
	content->setPosition(Point(m_size.width/2, m_size.height/2));
	//设置ttf的文本域
	content->setDimensions(Size(this->m_size.width - 60, this->m_size.height / 2 - 120));
	//设置ttf的水平对齐方式
    //content->setHorizontalAlignment(kCCTextAlignmentLeft);
	content->setColor(Color3B::MAGENTA);
	m_bgSprite->addChild(content);
}
